﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using DA;

namespace BL
{
    public class ERPMembership
    {
        public enum LoginStatus
        {
            Success,
            UserIsNotExist,
            PasswordNotMatch,
            UserIsLocked,
            UserIsNotActive,
            LoginFailed
        }
        /*
        public KeyValuePair<LoginStatus, ERP_Users> Login(string Email, string Password)
        {
            ERP_Users d = new DataRepository<ERP_Users>();
            ERP_Users loggedERP_Users = d.GetEntity(x => (x.Email.ToLower() == Email.ToLower()));
            if (loggedERP_Users == null)
                return new KeyValuePair<LoginStatus, ERP_Users>(LoginStatus.ERP_UsersIsNotExist, loggedERP_Users);
            else if (!loggedERP_Users.isActive)
                return new KeyValuePair<LoginStatus, ERP_Users>(LoginStatus.ERP_UsersIsNotActive, loggedERP_Users);
            else if (loggedERP_Users.isLockedOut)
                return new KeyValuePair<LoginStatus, ERP_Users>(LoginStatus.ERP_UsersIsLocked, loggedERP_Users);
            else if (loggedERP_Users.Password != EncryptionFormatter.EncryptionFormats[loggedERP_Users.Encryption].ComputeHash(EncryptionFormatter.EncryptionFormats[loggedERP_Users.Encryption].ComputeHash(Password)))
            {
                loggedERP_Users.LoginAttemps += 1;
                if (isLocked(loggedERP_Users))
                    loggedERP_Users.isLockedOut = true;
                d.Save(loggedERP_Users);
                //Added the following 2 lines, to show the ERP_Users he has been locked on the 5th time when he's locked, instead of giving him another login attempt that will always fail since he's locked (the locked state wasnt returned right away)
                if (isLocked(loggedERP_Users))
                    return new KeyValuePair<LoginStatus, ERP_Users>(LoginStatus.UserIsLocked, loggedERP_Users);
                return new KeyValuePair<LoginStatus, ERP_Users>(LoginStatus.PasswordNotMatch, d.Save(loggedERP_Users));
            }
            else if (loggedERP_Users.Password == EncryptionFormatter.EncryptionFormats[loggedERP_Users.Encryption].ComputeHash(EncryptionFormatter.EncryptionFormats[loggedERP_Users.Encryption].ComputeHash(Password)))
            {
                loggedERP_Users.LastLogin = DateTime.Now;
                loggedERP_Users.LoginAttemps = 0;
                return new KeyValuePair<LoginStatus, ERP_Users>(LoginStatus.Success, d.Save(loggedERP_Users));
            }
            else
                return new KeyValuePair<LoginStatus, ERP_Users>(LoginStatus.LoginFailed, d.Save(loggedERP_Users));
        }*/

        private HttpCookie PermanentCookie(ERP_Users ERP_Users, string cookieName, string cookieERP_UsersIdKey, DateTime? cookieExpirationTime = null)
        {
            HttpCookie permanentCookie = new HttpCookie(cookieName);
            permanentCookie.Values[cookieERP_UsersIdKey] = ERP_Users.UserId.ToString();
            permanentCookie.Expires = cookieExpirationTime.HasValue ? cookieExpirationTime.Value : DateTime.Now.AddYears(1);

            return permanentCookie;
        }

        private void CreatePermanentCookie(ERP_Users ERP_Users, string cookieName, string cookieERP_UsersIdKey, DateTime? cookieExpirationTime = null)
        {
            HttpContext.Current.Response.Cookies.Add(PermanentCookie(ERP_Users, cookieName, cookieERP_UsersIdKey,  cookieExpirationTime));
        }
    }
}
